/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eighteen12.web.controller;

import com.eighteen12.web.Controller;
import com.eighteen12.web.controller.vo.CurrencyVo;
import com.eighteen12.web.controller.vo.Utilities;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

/**
 *
 * @author Arif
 */
public class CurrenciesBca implements Controller<Object> {

    public Object execute(HttpServletRequest request) {
        CurrencyVo result = new CurrencyVo();
        result.bankCode = "BCA";
        result.bankName = "Bank Central Asia";
        try {
            Document doc = Jsoup.connect("http://www.bca.co.id/en/kurs-sukubunga/kurs_counter_bca/kurs_counter_bca_landing.jsp").get();
            Elements elements = doc.getElementsByTag("table").get(3).getElementsByTag("tr").get(2).getElementsByTag("td");
            result.dollarJual = Double.parseDouble(elements.get(0).text().replaceAll("\\.", "")) / 100;
            result.dollarBeli = Double.parseDouble(elements.get(1).text().replaceAll("\\.", "")) / 100;
            result.lastUpdate = new Date();
            new Utilities().storeToDb(result);
        } catch (IOException ex) {
            Map<String,String> message = new HashMap<String, String>();
            message.put("error", ex.getMessage());
            return message;
        }
        return result;
    }

    

}
